package org.xbzheng.restful_study.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xbzheng.restful_study.model.Resources;
import org.xbzheng.restful_study.service.ResourceService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    @RequestMapping("/")
    public String index(HttpServletRequest request){
        List<Resources> resources = resourceService.findAll();
        request.setAttribute("resources", resources);
        return "/admin/resource/index";
    }

    @RequestMapping("/create")
    public String create(Resources resources){
        resourceService.save(resources);
        return "redirect:/resource/";
    }

    @RequestMapping("/remove")
    @ResponseBody
    public String remove(List<Resources> resources){
        resourceService.remove(resources);
        return "true";
    }

}
